package org.desktop.web.client.field.validator;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

import com.google.gwt.editor.client.Editor;
import com.google.gwt.editor.client.EditorError;
import com.google.gwt.regexp.shared.RegExp;
import com.sencha.gxt.widget.core.client.form.Validator;
import com.sencha.gxt.widget.core.client.form.error.DefaultEditorError;
import com.sencha.gxt.widget.core.client.form.validator.RegExValidator;

public class EmailValidator implements Validator<String>{

    private final static String regex = "^([\\w\\-\\.]+)@((\\[([0-9]{1,3}\\.){3}[0-9]{1,3}\\])|(([\\w\\-]+\\.)+)([A-Za-z]{2,4}))$";
    private final static RegExp pattern = RegExp.compile(regex);
    private final static String type = "email address";
	@Override
	public List<EditorError> validate(Editor<String> editor, String value) {
		List<EditorError> errors = new ArrayList<EditorError>();
		if(!pattern.test(value)){
			errors.add(new DefaultEditorError(editor, "Indirizzo email non valido!", value));
		}
		
		return errors;
	}
	
	public static boolean isValid(String testEmail){
		return pattern.test(testEmail);
	}

}

